package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.MyAcquisitionDto;
import zhihuilan.dto.MyStaffMsgDto;
import zhihuilan.dto.PageDto;
import zhihuilan.entity.MyStaffMsg;
import zhihuilan.entity.MyStaffMsgRoom;
import zhihuilan.mapper.MyStaffMsgMapper;
import zhihuilan.params.query.MyStaffMsgQueryParam;
import zhihuilan.params.query.MyStaffMsgRoomQueryParam;
import zhihuilan.service.MyStaffMsgService;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyStaffMsgServiceImpl extends ServiceImpl<MyStaffMsgMapper, MyStaffMsg> implements MyStaffMsgService {


    @Resource
    private MyStaffMsgMapper myStaffMsgMapper;
    /**
     * 查询用户消息信息
     *

     * @return 用户信息集合
     */
    @Override
    public PageDto<MyStaffMsg> selectMyStaffMsgList(MyStaffMsgQueryParam myStaffMsg){
        QueryWrapper<MyStaffMsg> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(myStaffMsg)) {
//            if (!ObjectUtils.isEmpty(myStaffMsg.getGuid())) {
//                queryWrapper.eq("guid", myStaffMsg.getGuid());
//            }
            if (!ObjectUtils.isEmpty(myStaffMsg.getStaffId())) {
                queryWrapper.eq("staff_id", myStaffMsg.getStaffId());
            }
            if (!ObjectUtils.isEmpty(myStaffMsg.getRoomId())) {
                queryWrapper.eq("room_id", myStaffMsg.getRoomId());
            }
            if (!ObjectUtils.isEmpty(myStaffMsg.getTime())) {
                queryWrapper.gt("create_time", myStaffMsg.getTime());
            }
        }
        queryWrapper.orderByDesc("create_time");
//        Long pageNum=0l,pageSize=0l;
//        if (myStaffMsg.getPage() >= 0L) {
//            pageNum = myStaffMsg.getPage();
//        }
//        if (myStaffMsg.getPageSize() > 0L) {
//            pageSize = myStaffMsg.getPageSize();
//        }

        PageDto<MyStaffMsg> page = new PageDto<>(myStaffMsg.getPage(), myStaffMsg.getPageSize());
        baseMapper.selectPage(page,queryWrapper);

            List<MyStaffMsg> convertList = page.getRecords().stream().map(po -> {
                MyStaffMsg myStaffMsgDto = new MyStaffMsg();
                BeanUtils.copyProperties(po, myStaffMsgDto);
                myStaffMsgDto.setUserType("friend");
                if(myStaffMsgDto.getSender().equals(myStaffMsg.getStaffId())){
                    myStaffMsgDto.setUserType("self");
                }
                return myStaffMsgDto;
            }).collect(Collectors.toList());
        Collections.reverse(convertList); // 直接反转原列表
        page.setRecords(convertList);
        return page;
    }

    /**
     * 根据ID查询用户消息；
     * */
    @Override
    public MyStaffMsg selectMyStaffMsgById(Integer id){
        return baseMapper.selectById(id);
    }


    @Override
    public MyStaffMsg selectMyStaffMsg(MyStaffMsgQueryParam myStaffMsg){
        QueryWrapper<MyStaffMsg> queryWrapper = new QueryWrapper<>();
        Integer id = myStaffMsg.getSender();
        if(myStaffMsg.getStaffId().equals(id)){
            id = myStaffMsg.getRecipient();
        }
        if(!ObjectUtils.isEmpty(myStaffMsg.getStaffId())){
            queryWrapper.eq("staff_id",myStaffMsg.getStaffId());
        }
        if(!ObjectUtils.isEmpty(myStaffMsg.getSender())&&!ObjectUtils.isEmpty(myStaffMsg.getRecipient())){
            Integer finalId = id;
            queryWrapper.nested(qw->qw.eq("sender", finalId).or().eq("recipient", finalId));

          //  queryWrapper.eq("sender|recipient",myStaffMsg.getSender());
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 删除消息
     *
     * */
    @Override
    public void delMyStaffMsgById(String id){
        baseMapper.deleteById(id);
    }

    /**
     * 新增用户消息
     * */
    @Override
    public Integer addEntity(MyStaffMsgQueryParam entity){
        MyStaffMsg myStaffMsg = new MyStaffMsg();
        BeanUtils.copyProperties(entity,myStaffMsg);
        return baseMapper.insert(myStaffMsg);
    }

    @Override
    public Integer updateEntity(MyStaffMsgQueryParam entity){
        QueryWrapper<MyStaffMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",entity.getRoomId());
        queryWrapper.eq("staff_id",entity.getStaffId());
        MyStaffMsg myStaffMsg = new MyStaffMsg();
        BeanUtils.copyProperties(entity,myStaffMsg);
        return baseMapper.update(myStaffMsg,queryWrapper);
    }
    /**
     * 更新用户消息
     * */
    @Override
    public  Integer updateReadEntity(MyStaffMsgQueryParam entity){
        QueryWrapper<MyStaffMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",entity.getRoomId());
        queryWrapper.eq("staff_id",entity.getStaffId());
        MyStaffMsg myStaffMsg = new MyStaffMsg();
        myStaffMsg.setStatus(0);
        return baseMapper.update(myStaffMsg,queryWrapper);
    }

    @Override
    public Long getRedCountMsg(MyStaffMsgQueryParam queryParam){
        QueryWrapper<MyStaffMsg> queryWrapper = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(queryParam.getStaffId())) {
            queryWrapper.eq("staff_id", queryParam.getStaffId());
        }
        if(!ObjectUtils.isEmpty(queryParam.getRecipient())) {
            queryWrapper.eq("recipient", queryParam.getRecipient());
        }
        if(!ObjectUtils.isEmpty(queryParam.getSender())) {
            queryWrapper.eq("sender", queryParam.getSender());
        }
        queryWrapper.eq("status",1);
        return  baseMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer delMyStaffMsg(MyStaffMsgQueryParam queryParam){
        QueryWrapper<MyStaffMsg> queryWrapper = new QueryWrapper<>();
        if(queryParam.getStaffId()>0){
            queryWrapper.eq("staff_id",queryParam.getStaffId());
        }
        if(queryParam.getRoomId()!=null){
            queryWrapper.eq("room_id",queryParam.getRoomId());
        }
        return baseMapper.delete(queryWrapper);
    }
}
